<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2018/3/28
 * Time: 17:38
 */

namespace app\admin\logic;


use app\common\utils\WeChatUtils;

class MessageLogic
{

    static private $instance;
    private  $url = "http://www.726bh9.cn/wuye/jiaofei.html";
    static public function getInstance(){
        if (!self::$instance instanceof self) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    public function sendMessageByHousePropertyPay($ids){
        $weChatUtils = WeChatUtils::getInstance();
        //获取未缴费房产
        if ($ids) {
            $map['id'] = array('in', $ids);
            $housePropertyList = model('HouseProperty')->where($map)->select();
        }
        else{
            $housePropertyList = [];
        }
        foreach ($housePropertyList as $key => $value) {
            if(!$value && !isset($value['user_id']) && empty($value['user_id']) && $value['water_charge'] == 0 && $value['property_charge'] == 0){
                continue;
            }
            //获取openid
            $openid = model('User')->where(array('id'=>$value['user_id']))->value('openid');
            if (!empty($openid) && $openid != '') {
                $village = model('Village')->where(array('id'=>$value['village_id']))->value("name");
                $message = "{$village}小区{$value['detail']}（地址）  未缴费用为：1.物业费 {$value['property_charge']}元；水费{$value['water_charge']}元。<a href='" . $this->url ."?house_property_id={$value['id']}" . "'>前往缴费</a>";
                $weChatUtils->sendCustomerMessage($openid, $message);
                model('HouseProperty')->where(array('id'=>$value['id']))->setField('notice_time', time());
            }
        }
    }
}